<?
include "include/config.php";
$location='oferte'; 

if(!isset($_SESSION['_user'])) { 
header("location: login.php");
}

if($_SESSION['_level']==0) { 
	header("location: afisare_oferta.php");
}
////cautare locatie
$loc=$_REQUEST['loc'];
$tip=array ('1'=>'Garsoniera','2'=>'Apartament 2 camere','3'=>'Apartament 3 camere','4'=>'Apartament 4 camere','5'=>'Teren','6'=>'Spatiu Comercial','7'=>'Casa/Vila');
$valuta=array ('0'=>'Discutabil','1'=>'RON','2'=>'EURO','3'=>'USD'); 
$oferta=array ('1'=>'Vanzare','2'=>'Inchiriere');
if ($_POST['loc']) {
	if ($_POST['spof']=='on')
	$spof=1;
	else $spof=0;


    mysql_query("insert into oferta (tip,oferta,id_loc,pret,pret_tip,detalii,subzona,spof,user_id,subiect) values ('". $_POST['tip']."','".$_POST['oferta']."','". $_POST['loc']."','". $_POST['pret']."','". $_POST['valuta']."','".$_POST['detalii']."','".$_POST['nume_locatie']."','".$spof."', '".$_SESSION['_id'] ."' , '".$_POST['subiect']."')");

    $id_oferta=mysql_insert_id();
    // IMAGE CROP
    $sql=mysql_query("select width_t,height_t,width,height,width_m,height_m from setari");

    list($width_t,$height_t,$width_n,$height_n,$width_m,$height_m)=mysql_fetch_array($sql);
	$c=0;  
	for($i=0;$i<=10;$i++) {	
	    if($_FILES['pictures'.$i]['name']!='') {	
                        
                list($width, $height, $type, $attr) = getimagesize($_FILES['pictures'.$i]['tmp_name']);						
		switch($type) {
		case 1:
			$img=imagecreatefromgif($_FILES['pictures'.$i]['tmp_name'])
			or die('xxx');
			break ;
		case 2:
			$img=imagecreatefromjpeg($_FILES['pictures'.$i]['tmp_name'])
			or die('xxx');
			break ;
		case 3:
			$img=imagecreatefrompng($_FILES['pictures'.$i]['tmp_name'])
				or die('xxx');
			break ;
		default:
			break 2;	
		} // thumbs 
		mysql_query("insert into `imagini` (id_oferta) values('".$id_oferta."')");
                $id_poza=mysql_insert_id();
		if($width_t/$height_t<$width/$height) {
			$_width=$width_t;
			$_height=(int)($height*$width_t/$width);
		}
		else {
			$_height=$height_t;
			$_width=(int)($width*$height_t/$height);
		}
					
		$image=imagecreatetruecolor($_width,$_height);
		imagecopyresampled($image,$img,0,0,0,0,$_width+1,$_height+1,$width-1,$height-1);
		$imagename=$id_oferta.'_'.$id_poza.".jpg";
		//imageantialias($image, true);
		imagejpeg($image,substr(srvd, 0, -6)."imagini/thumbs/".$imagename,100);
		// normal
		if($width_n/$height_n<$width/$height) {
			$_width=$width_n;
			$_height=(int)($height*$width_n/$width);
		}
		else {
			$_height=$height_n;
			$_width=(int)($width*$height_n/$height);
		}
		$image=imagecreatetruecolor($_width,$_height);
		imagecopyresampled($image,$img,0,0,0,0,$_width+1,$_height+1,$width-1,$height-1);
		//imageantialias($image, true);
		imagejpeg($image,substr(srvd, 0, -6)."imagini/normale/".$imagename,100);
		// mare
		if($width_m/$height_m<$width/$height) {
				$_width=$width_m;
				$_height=(int)($height*$width_m/$width);
		}
		else {
			$_height=$height_m;
			$_width=(int)($width*$height_m/$height);
		}
		$image=imagecreatetruecolor($_width,$_height);
		imagecopyresampled($image,$img,0,0,0,0,$_width+1,$_height+1,$width-1,$height-1);
		//imageantialias($image, true);
		imagejpeg($image,substr(srvd, 0, -6)."imagini/mari/".$imagename,100);
		mysql_query("UPDATE imagini SET src='".$imagename."' WHERE id='".$id_poza."'");
						
	    } 
	}
	$final='Oferta introdusa cu succes !';
    }
    $loc_query=mysql_query("select id,name from locatii where id='".$loc."'");
    $loc_vect=mysql_fetch_array($loc_query);

 include "include/head.php";
?>

<div id="container">
<? include "include/meniu.php"; ?>

<div id="submenu"><div><div>
    <ul>
        <li id="off2"><a href="">Adauga Oferta</a></li>
        <li ><a href="afisare_oferta.php">Vizualizare Oferte</a></li>
        
    </ul>
</div></div></div>
<div class="clear"></div>
<div id="content"><div id="mid">
<div>
<form enctype="multipart/form-data" action="adauga_oferta.php" method="post" >
<br class="clear"/>
<h2>Formular Adaugare Oferta</h2>
<div class="menu">
<ul>
    <li class="sub"><a href="#nogo"><input type="text" readonly name="nume_locatie" id="nume_locatie" value=zona class="tf b"/> </a>
<?=meniu('0'); ?> 
    </li>
</ul>
</div> <br>
<input type="hidden" name="loc" id="loc" />


    <br />
    <label>Descriere oferta </label>
    <input type="text" name="subiect"  class="tf bbb"/> <br />
    
    <label>Tip</label>
    <select name='tip' class='bb'>
    <? foreach ($tip as $key=>$value) 
        echo '<option value='.$key.'>'.$value.'</option> '; ?>
    </select>     <br />
    
    <label>Oferta</label>
    <select name='oferta' class='bb'>
    <? foreach ($oferta as $key=>$value) 
        echo '<option value='.$key.'>'.$value.'</option> '; ?>
    </select> <br />
    
    <label>Pret </label>
    <input type="text" name="pret"  class="tf mm"/> 
    
    <select name='valuta' class='b'>
    <? foreach ($valuta as $key=>$value) 
        echo '<option value='.$key.'>'.$value.'</option> '; ?>
    </select><br />
    <label>Oferta speciala</label><input type="checkbox" name="spof"  /><br />

    <label>Detalii</label>
    <textarea type="text" name="detalii"  cols="100" rows="10" class="tf "/></textarea>
    
    <br class="clear" />
    <label>Adauga imagini</label>
    <input name="pictures0" id="sel_vals_0" type="file" class="tf bb" onchange="document.getElementById('pic1').style.display='block';" />
    <? for($i=1;$i<=10;$i++) { ?>
    <div id="pic<?=$i?>" style="display:none; width:600px;">
        <label></label>
        <input name="pictures<?=$i?>" type="file" class="tf bb " onchange="document.getElementById('pic<?=($i+1)?>').style.display='block';" />
    </div>
    <? }?>

<br class="clear" />
<br class="clear" />
<label>&nbsp;</label>
<input type="submit" value="ok" name="Adauga" class="ok"/>
</form>

<? if ($final!='') {?>
<br class="clear" />
<br class="clear" />
<div class="cell__ bbbbb ac"><strong><?=$final?></strong></div>
<?}?>
</div>
</div>
</div>
<? include "include/footer.php";  ?> 